package com.sap.sap_mangers.controller;

import com.sap.sap_mangers.Vo.ActivityLlkVo.ActivityLlkVo;
import com.sap.sap_mangers.dto.ActivityLlk.ActivityLlkSubmit;
import com.sap.sap_mangers.pojo.ActivityLlk;
import com.sap.sap_mangers.service.ActivityLlkService;
import com.sap.sap_mangers.until.JwtUtil;
import com.sap.sap_mangers.until.SimpleJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 2021年女生节连连看活动记录 controller
 *
 * @author ZhouKai
 * 2022/8/2 16:45
 */
@RestController
public class ActivityLlkController {

    ActivityLlkService activityLlkService;

    @Autowired
    public ActivityLlkController(ActivityLlkService activityLlkService) {
        this.activityLlkService = activityLlkService;
    }

    /**
     * 提交活动记录
     *
     * @param submit 前端请求
     * @return 成功
     */
    @PreAuthorize("hasAuthority('正式成员')")
    @PostMapping("/ActivityLlk/submit")
    public SimpleJson submitRecord(@RequestBody ActivityLlkSubmit submit) {
        //生成记录 (id和update在mysql中自动生成，uid通过Token解析获取)
        ActivityLlk activityLlk = new ActivityLlk(null, JwtUtil.getUserForToken(), submit.getGameWin(), submit.getScore(), null, submit.getTime());
        activityLlkService.submitRecord(activityLlk);
        return SimpleJson.SUCCESS;
    }

    /**
     * 获取用户自己的活动记录
     *
     * @return 查询情况
     */
    @PreAuthorize("hasAuthority('正式成员')")
    @GetMapping("/ActivityLlk/getOwnRecord")
    public SimpleJson getOwnRecord() {
        List<ActivityLlk> ownRecord = activityLlkService.getOwnRecord(JwtUtil.getUserForToken());
        if (ownRecord.isEmpty()) {
            return new SimpleJson(1, "您没有参与过活动");
        } else {
            return new SimpleJson(0, "成功获取到记录", ownRecord);
        }
    }

    /**
     * 获取活动排名(包含详细信息)
     *
     * @return 活动排名(包含详细信息)
     */
    @PreAuthorize("hasAuthority('正式成员')")
    @GetMapping("/Activity/getRecordRank")
    public SimpleJson getRecordRank() {
        List<ActivityLlkVo> recordRank = activityLlkService.getRecordRank();
        return new SimpleJson(0, "成功", recordRank);
    }
}
